<?php

/**
 * RP System
 *
 * @version 1.0
 * @copyright 2004 Ron Planken
 */

class raid {

	function get($raid_id = 0, $status = 0,  $limit = 0, $user_id = 0) {

		global $dbh;

		if ($user_id > 0) {
			$query ="SELECT  r.raid_id
							,p.name
							,r.date_time
							,r.freeze_time
							,r.no_players
							,r.substitute_max
							,r.class_max
							,r.description
							,e.name as 'event_name'
							,r.leader_player_id
							,r.mt1_player_id
							,r.mt2_player_id
							,r.event_id
							,r.status
							,r.main_team_id
							,t1.team_order as 'main_team_order'
							,r.sub_team_id
							,t2.team_order as 'sub_team_order'
							,r.rp_category_id
					   FROM raid r
					   JOIN turnin t ON t.raid_id = r.raid_id
					   JOIN player p ON p.player_id = t.player_id
					   JOIN event e ON e.event_id = r.event_id
					   JOIN team t1 ON t1.team_id = r.main_team_id
					   JOIN team t2 ON t2.team_id = r.sub_team_id
					  WHERE p.user_id = $user_id
						AND t.status = 0
							";
			if ($status == 0) {
				$query .= ' AND r.date_time > Now()';
				$query .= ' ORDER by r.date_time ASC';
			} else {
				$query .= ' AND r.date_time < Now()';
				$query .= ' ORDER by r.date_time DESC';
			}
		} elseif ($raid_id == 0) {
			$query ='SELECT  r.raid_id
							,r.date_time
							,r.freeze_time
							,r.no_players
							,r.substitute_max
							,r.class_max
							,r.description
							,p.name as "leader_name"
							,e.name as "event_name"
							,r.leader_player_id
							,r.mt1_player_id
							,r.mt2_player_id
							,r.event_id
							,r.status
							,r.main_team_id
							,t1.team_order as "main_team_order"
							,r.sub_team_id
							,t2.team_order as "sub_team_order"
							,r.rp_category_id
					   FROM raid r
					   JOIN team t1 ON r.main_team_id = t1.team_id
					   JOIN team t2 ON r.sub_team_id = t2.team_id
					   JOIN player p ON p.player_id = r.leader_player_id
					   JOIN event e ON e.event_id = r.event_id
							';
			if ($status == 0) {
				$query .= ' WHERE r.date_time > Now()';
				$query .= ' ORDER by r.date_time ASC';
			} else {
				$query .= 'WHERE r.date_time < Now()';
				$query .= ' ORDER by r.date_time DESC';
			}
			if ($limit > 0) {
				$query .= " LIMIT 0, $limit";
			}

		} else {
			$query ="SELECT  r.raid_id
							,r.date_time
							,r.freeze_time
							,r.no_players
							,r.substitute_max
							,r.class_max
							,r.description
							,p.name as \"leader_name\"
							,e.name as \"event_name\"
							,r.leader_player_id
							,r.mt1_player_id
							,r.mt2_player_id
							,r.event_id
							,r.status
							,r.main_team_id
							,r.sub_team_id
							,r.rp_category_id
					   FROM raid r, player p, event e
					  WHERE p.player_id = r.leader_player_id
					    AND e.event_id = r.event_id
					    AND r.raid_id = $raid_id
							";
		}

		$raids = selectMultiQuery($query, $dbh);

		return $raids;
	}

	function event($raid_id = 0) {

		global $dbh;

			$query ="SELECT	e.event_id
						   ,e.name
						FROM event e
					   WHERE status = 0;
							";


		$events = selectSingleTextQuery($query, $dbh);

		return $events;
	}

	function save($raid_id = 0, $date_time, $no_players, $substitute_max, $description, $leader_player_id, $mt1_player_id, $mt2_player_id, $event_id, $status, $main_team_id, $sub_team_id, $rp_category_id, $freeze_time = 0) {


		global $dbh;

		$raid_id = secure_int($raid_id);
		$date_time = secure_string($date_time);
		$no_players = secure_int($no_players);
		$substitute_max = secure_int($substitute_max);
		$description = secure_string($description);
		$leader_player_id = secure_int($leader_player_id);
		$mt1_player_id = secure_int($mt1_player_id);
		$mt2_player_id = secure_int($mt2_player_id);
		$event_id = secure_int($event_id);
		$status = secure_int($status);
		$main_team_id = secure_int($main_team_id);
		$sub_team_id = secure_int($sub_team_id);
		$rp_category_id = secure_int($rp_category_id);
		$freeze_time = secure_int($freeze_time);


		if ($raid_id == 0) {
			$query ="INSERT INTO raid
						    (date_time
						    ,freeze_time
							,no_players
							,substitute_max
							,description
							,leader_player_id
							,mt1_player_id
							,mt2_player_id
							,event_id
							,status
							,main_team_id
							,sub_team_id
							,rp_category_id)
					VALUES  ($date_time
							,$freeze_time
							,$no_players
							,$substitute_max
							,$description
							,$leader_player_id
							,$mt1_player_id
							,$mt2_player_id
							,$event_id
							,$status
							,$main_team_id
							,$sub_team_id
							,$rp_category_id)
							";
			
			$result = InsertQuery($query, $dbh);

			$status = mysql_insert_id($dbh);
			
		} else {
			$query ="UPDATE raid
						SET date_time = $date_time
							,freeze_time = $freeze_time
							,no_players = $no_players
							,substitute_max = $substitute_max
							,description = $description
							,leader_player_id = $leader_player_id
							,mt1_player_id = $mt1_player_id
							,mt2_player_id = $mt2_player_id
							,event_id = $event_id
							,status = $status
							,main_team_id = $main_team_id
							,sub_team_id = $sub_team_id
							,rp_category_id = $rp_category_id
					  WHERE raid_id = $raid_id
							";


			$status = UpdateQuery($query, $dbh);
		}

		return $status;

	}


	function remove($raid_id = 0) {

		global $dbh;

		$raid_id = secure_int($raid_id);

		$query ="DELETE FROM raid
				  	   WHERE raid_id = $raid_id;
							";
		$status = UpdateQuery($query, $dbh);

		return $status;
	}

}
?>